function ProcessDataFilesTS


path1 = '/Volumes/300GB iceCUBE G2/DataEmotion/EmotionExp';
path2 = '/Volumes/HD-LBU2/Student Data';

if exist(path1)
  pathtofiles = path1;
elseif exist(path2)
  pathtofiles = path2;
end


for E = 1:18
  excerpt= ['excerpt' num2str(E)];
  % collect all filenames that are for the correct excerpt
  demog = rdir([ pathtofiles '/**/*Demographics*.txt']);
  indices=[];
  for i = 1:length(demog);
    if strfind(demog(i).name,'ZAdult')
      indices = [indices i];
    end
  end
  demog(indices) = [];
  outResponseTime = [];
  num = 1;
  time = [];
  %%%
  % Start loop for first demographic file
  individualF = 1;
  individualE =1;
  wavfileProbs = 0;
  matE=[];
  matF=[];
  
  for demFile = 1:length(demog)
    %Progress
    fprintf('*********** File %d of %d *********** \n',demFile,length(demog));
    % read file
    disp(demog(demFile).name);
  
    if strfind(demog(demFile).name,'2011') 
      disp('2011 data, skipping'); 
      continue;
    end
    
    if strfind(demog(demFile).name,'Adlt') 
      disp('Adult File, skipping'); 
      continue;
    end
    
    if strfind(demog(demFile).name,'/.')
      disp(demog(demFile).name) 
      disp('Thats a hidden file right (skipping)');
      continue;
    end
    
    fidDem=fopen(demog(demFile).name);
     
    while 1
      dt= fgetl(fidDem);
      if ~ischar(dt), break, end
      lastLine = dt;
    end
    fclose(fidDem);
    [path,fndemog] = fileparts(demog(demFile).name);
    numID = strrep(fndemog,'-Demographics','');
    numIDPos = strfind(lastLine,numID);
    lastLine= lastLine(numIDPos(end):end);
    clear('SID');
    SIDpos = strfind(numID,'-');
    SID    = numID(SIDpos(end)+1:end);
    dems = textscan(lastLine,'%s%f%f%f%f%s%d%d%q%q%q%q');
    
    disp(SID);
    % Set variables for the demographics
    id = dems{1};
    age = dems{2} + dems{3}/12;
    mus = dems{4} + dems{5}/12;
    gender = dems{6};
    faveFace = dems{7};
    worstFace = dems{8};
    lang = [char(dems{9}) ' ' char(dems{10}) ' ' char(dems{11}) ' ' char(dems{12})];
    
    % get files for the excerpts
    fn = strrep(demog(demFile).name,'-Demographics','');
    fidExc = fopen(fn);
    if fidExc == -1
      warning(['couldn''t get at: ' fn]);
      % fclose(fidExc);
      continue
    end
    
    
    for line = 1:27
      exc = textscan(fidExc,'%s%s%q%q%q%q%q%s%q%q\n');
      if line <3
        continue;
      end
      if isempty(exc{2})
        break;
      end
      TimeRec(line) = exc{2};
      Know(line) = exc{3};
      KnowExtra(line) = exc{4};
      Like(line) = exc{5};
      LikeExtra(line) = exc{6};
      Level(line) = exc{7};
      Locus(line) = exc{8};
      excerpttmp(line)= exc{9};
      spacePos = cell2mat(strfind(exc{9},' '));
      spacePos(end+1) = length(exc{9}{1});
      exc{:};
      
      Excerpts(line) = {exc{9}{1}(spacePos(1)+1:spacePos(2)-1)};
      Preceding(line) = {'None'};
    end
    fclose(fidExc);
    
    % What about level 2???
    for line = 24:26
      TimeRec(line+4) = TimeRec(line);
      Know(line+4) = Know(line);
      KnowExtra(line+4) = KnowExtra(line);
      Like(line+4) = Like(line);
      LikeExtra(line+4) = LikeExtra(line);
      Level(line+4) = Level(line);
      Locus(line+4) = Locus(line);
      spacePos = strfind( excerpttmp{line},' ');
      spacePos(end+1) = length(excerpttmp{line});
      Excerpts(line+4) = {excerpttmp{line}(spacePos(2)+1:spacePos(3)-1)};
      Preceding(line+4) = Excerpts(line);
    end
    
    level3exc = excerpttmp{27};
    % What about Level 3 ?
    for line = 1:6
      TimeRec(30+line) = TimeRec(27);
      Know(30+line) = Know(27);
      KnowExtra(30+line) = KnowExtra(27);
      Like(30+line) = Like(27);
      LikeExtra(30+line) = LikeExtra(27);
      Level(30+line) = Level(27);
      Locus(30+line) = Locus(27);
      spacePos = strfind(level3exc,' ');
      spacePos(end+1) = length(level3exc);
      
      Excerpts(30+line) = {level3exc(spacePos(line+1)+1:spacePos(line+2)-1)};
      if line == 1
        Preceding(30+line) = Excerpts(27);
      else
        Preceding(30+line) = Excerpts(30+line-1);
      end
    end
    
    resFiles = rdir([pathtofiles '/**/' id{1} '*' '.aiff']);
    numfiles = length(resFiles);
    if numfiles == 0
      resFiles = rdir([pathtofiles '/**/' id{1} '*' '.wav']);
      numFiles2 = length(resFiles);
      if numFiles2 > numfiles
        wavfileProbs = wavfileProbs +1;
        disp(['>>>>>>>>>>>> Aiff Files: ' num2str(numfiles) ' Wav Files: ' num2str(numFiles2) ' Probs: ' num2str(wavfileProbs) ' <<<<<<<<<<<<<<']);
      end
      continue;
    end
    
    
    % For Level 2
    resFiles(26:28) = resFiles(22:24);
    % For Level 3
    resFiles(29:34) = resFiles(25);
    
    totalLevel3 = 0;
    % Start loop for the first excerpt
    
    
    for file = 1:length(resFiles)
      % Clear some stuff just in case
      data= [];
      time = [];
      dataTrue=[];
      dataAny = [];
      disp(resFiles(file).name);
      % Read the file details from the audio filename
      [folders,filename,extension]=fileparts(resFiles(file).name);
      fn = textscan([filename extension],'%d%d%d%d%d%s%s%d%s','delimiter','-@');
      currentfile = file;
      
      % Skip the Instructions and Level 3 (for now)
      if currentfile  > 34 ||  currentfile  < 2
        continue;
      elseif  currentfile  >= 8 || currentfile  <= 24
        presFormat = 'Excerpt';
      elseif  currentfile  > 1 || currentfile  <= 7
        presFormat = 'Training';
      end
    end
    
    
    % Load them one at a time and make a XY*subjects*length matrix
    
   
    
    % The following line looks at the current file 'excerpt' and then looks
    % through the list of files for this participant 'Excerpts' and checks
    % gives the indexes of the TWO positions in which it will exist
    % (Level1A and Level1B). These positions are stored as index(1) and
    % index(2). 
    % Below we will then look at 'resFiles', which is a list of actual filenames that 
    % corresponds to the list of Excerpt names 'Excerpts', and choose the
    % two filenames to load and convert to timeseries data.
    
    index = find(strcmp(Excerpts,excerpt))-2;
    
    % Check that MaxMSP has generated two blank lines. 
    % 
    
    
    Loci=Locus(index+2); % '+2' is because Max/MSP generated two 'blank' [no data] lines at the top of the file.
    len = 750; % 30 seconds
    fprintf('Num Indexs %d',length(index))
    if (length(index) == 1 && index(1) > 26)
      index(1)
      [data,fs] = aiffread(resFiles(index(1)).name);     % Get excerpt audio data
      data = data(:,3:4)/32768;       % Translate the x,y position of mouse to 0-1
      % need to do this because now reading mouse movement stored as 16bit
      % (44.1kHz) that is to be converted BACK to 0-1 as Max did.
      data = data(1:fs/25:end,:); % Decimate to 25fps before the matrix
      % the above command 'data = data(1:fs/25:end,:);' was used because it
      % is still a good fs (25Hz), and is useful for video generation.
      % However, to generate different fs, change the 25 to the desired fs.
      % This makes Sam nervous because, for example, fs=4Hz might require
      % something more sophisticated than decimation.  But it might be OK.
      % Could also try the resample command and compare with the above data
      % command.
         
    
      
      
      
 
      
      
      
      %THe above downsampling is done AGAIN for processing purposes.  That
      %is, felt data and expressed data can be handled separately.  This
      %way the data are grouped into two cells - 
      if strcmp(Loci{1},'Felt')
        matF(individualF,:,1:2) = data(1:len,:);
        individualF = individualF+1;
      elseif strcmp(Loci{1},'Expressed')
        matE(individualE,:,1:2) = data(1:len,:);
        individualE = individualE+1;
      end
      
      
      % NOW start again with the other file of the same excerpt in a
      % different locus.
      [data,fs] = aiffread(resFiles(index(2)).name);     % Get excerpt audio data
      data = data(:,3:4)/32768;       % Translate to 0-1
      data = data(1:fs/25:end,:); % Decimate to 25fps before the matrix
      
      
      if strcmp(Loci{2},'Felt')
        matF(individualF,:,1:2) = data(1:len,:);
        individualF = individualF+1;
      elseif strcmp(Loci{2},'Expressed')
        matE(individualE,:,1:2) = data(1:len,:);
        individualE = individualE+1;
      end
      
      
      KnowPoss = {'No','NotSure','HeardItSomewhere','Yes','empty'};
      LikePoss = {'No','NotSure','Maybe','Yes','empty'};
      
      outId{individualE}           = SID;
      outGender{individualE}       = gender{1};
      outAge(individualE)          = age;
      outMus(individualE)          = mus;
      outFaveFace(individualE)     = faveFace;
      outWorstFace(individualE)    = worstFace;
      outLang{individualE}         = lang;
      outKnow1(individualE)        = find(strcmp(Know(index(1)+2),KnowPoss));
      outKnow2(individualE)        = find(strcmp(Know(index(2)+2),KnowPoss));
      outLike1(individualE)        = find(strcmp(Like(index(1)+2),LikePoss));
      outLike2(individualE)        = find(strcmp(Like(index(2)+2),LikePoss));
      outLevel1{individualE}       = Level{index(1)+2};
      outLevel2{individualE}       = Level{index(2)+2};
      outLocus1{individualE}       = Locus{index(1)+2};
      outLocus2{individualE}       = Locus{index(2)+2};
      outExcerpt{individualE}      = excerpt;
      
    end
  end
  matE(matE==0) = NaN;
  matF(matF==0) = NaN;
  
  % Filter for Locus
  % Filter for other factors
  emotions = {'Elsewhere','Angry','Scared','Sad','Calm','Happy','Excited','Centre'};
  
  
  % Create the Timeseries Data Files
  if 1
    fidtile = fopen([excerpt 'TileChildren2010.txt'],'w');
    % creates time series data files.
    fprintf(fidtile,'Time\tID\tLocus\tEmotion\tLike\tKnow\tLevel\n'); % top line of the output file (header)
    for i = 2:length( matF(:,1,1)) %matrix for felt condition response
      if strcmp(outLocus1{i} , 'Felt')
        LikeString = LikePoss{outLike1(i)};
        KnowString = KnowPoss{outKnow1(i)};
        LevelString = outLevel1{i};        
      else
        LikeString = LikePoss{outLike2(i)};
        KnowString = KnowPoss{outKnow2(i)};
        LevelString = outLevel2{i};
      end
      
      dataCat = assessEmotionDataCategorical(squeeze(matF(i,:,1:2)));
      % above converts x,y co-ordinates into categorical face/space names.
      for j = 1:len
        fprintf(fidtile,'%.2f\t%s\tFelt\t%s\t%s\t%s\t%s\n', (j-1)/25, outId{i},emotions{dataCat(j)+1},LikeString,KnowString,LevelString);
      % writes data to the file, below the headers.
      end
    end
    
    for i = 2:length( matE(:,1,1))
      % see above comments, but now for Expressed condition.
      if strcmp(outLocus1{i} , 'Expressed')
        LikeString = LikePoss{outLike1(i)};
        KnowString = KnowPoss{outKnow1(i)};
        LevelString = outLevel1{i};
      else
        LikeString = LikePoss{outLike2(i)};
        KnowString = KnowPoss{outKnow2(i)};
        LevelString = outLevel2{i};
      end
      
      dataCat = assessEmotionDataCategorical(squeeze(matE(i,:,1:2)));
      for j = 1:len
        fprintf(fidtile,'%.2f\t%s\tExpressed\t%s\t%s\t%s\t%s\n', (j-1)/25, outId{i},emotions{dataCat(j)+1},LikeString,KnowString,LevelString);
      end
    end
    fclose(fidtile); % closes file, and loops to commence next 'excerpt' response output file processing.
  end
  
  
  
  % optionally can generate a video of the expressed/felt time series
  % responses from above. Set if to '1' to activate.  Set to '0' o ignore
  % Warning: slows processing time CONSIDERABLY.  Approximately 10 hours.
  % Note: only visual is generated.  Need to add audio, e.g. via Quicktime
  % Pro.
  if 0
    % Make Video
    % Read Video file
    vidWriter    = VideoWriter([excerpt '.avi']);
    set(vidWriter,'FrameRate',25);
    open(vidWriter);
    
    
    framestep = fs/25;
    
    % Make figure of particular size
    scrsz = get(0,'ScreenSize');
    h = figure('Position',[1 scrsz(4)/1.5 scrsz(3)/3 scrsz(4)/1.5]);
    im = imread('FacialImages.jpg');
    % Loop
    for i = 4:len
      % Make two subplots
      % Plot each frame of the matrix succesively, as a line with previous points connected.
      clf;
      subplot(2,1,1);
      imagesc([0.25 0.75], [0.2 0.8], im)
      hold on;
      plot(matF(:,i,1),matF(:,i,2),'wo');
      plot(matF(:,i-3:i,1)',matF(:,i-3:i,2)','w');
      set(gca,'YDir','reverse')
      xlabel('Valence');
      ylabel('Arousal');
      axis([0.25 0.75 0.25 0.75]);
      title(['Felt Locus, Excerpt ' num2str(E) ', ' num2str(length(matF(:,i,2))) ' participants. Analysis EMRG, UNSW.' ]);
      set(gca,'XTick',[])
      set(gca,'YTick',[])
      
      
      subplot(2,1,2);
      imagesc([0.25 0.75], [0.2 0.8], im)
      hold on;
      plot(matE(:,i,1),matE(:,i,2),'wo');
      plot(matE(:,i-3:i,1)',matE(:,i-3:i,2)','w');
      set(gca,'YDir','reverse')
      
      axis([0.25 0.75 0.25 0.75]);
      xlabel('Valence');
      ylabel('Arousal');
      title(['Expressed Locus, Excerpt ' num2str(E) ', ' num2str(length(matE(:,i,2))) ' participants. Analysis EMRG, UNSW.' ]);
      set(gca,'XTick',[])
      set(gca,'YTick',[])
      
      currFrame = getframe(h);
      writeVideo(vidWriter,currFrame);
    end
    close(vidWriter);
  end
  
  % Colour differently, depending on face and time.
  % work through matE and matF comparing positioning to test whether there is a match.
  % Do categorically, and continuously with pythagoras
  
  % First, are the matrices the same size?
  fprintf('E: %d, F: %d\n',individualE,individualF);
  if 0
  % OK set up a loop
  for i = 1:individualE-1
    [match(i)] = assessEmotionDataLongitude(squeeze(matE(i,:,:)),squeeze(matF(i,:,:)),excerpt);
    
  end
  
  for i = 1:individualE-1
    fprintf(fid,'%.3f\t%s\t%s\t%f\t%f\t%d\t%d\t%s\t%d\t%d\t%d\t%d\t%s\t%s\t%s\t%s\t%s\n',...
      match(i),...
      outId{i},...
      outGender{i},...
      outAge(i),...
      outMus(i),...
      outFaveFace(i),...
      outWorstFace(i),...
      strrep(outLang{i},' ',''),...
      outKnow1(i),...
      outKnow2(i),...
      outLike1(i),...
      outLike2(i),...
      outLevel1{i},...
      outLevel2{i},...
      outLocus1{i},...
      outLocus2{i},...
      outExcerpt{i});
  end
  end
  fclose('all')
end

%fclose(fid);
